1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect;
18
19 import static com.google.common.collect.CollectPreconditions.checkNonnegative;
20
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.base.Supplier;
23
24 import java.io.Serializable;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import javax.annotation.Nullable;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 @GwtCompatible(serializable = true)
56 public class HashBasedTable<R, C, V> extends StandardTable<R, C, V> {
57 private static class Factory<C, V>
58 implements Supplier<Map<C, V>>, Serializable {
59 final int expectedSize;
60 Factory(int expectedSize) {
61 this.expectedSize = expectedSize;
62 }
63 @Override
64 public Map<C, V> get() {
65 return Maps.newHashMapWithExpectedSize(expectedSize);
66 }
67 private static final long serialVersionUID = 0;
68 }
69
70
71
72
73 public static <R, C, V> HashBasedTable<R, C, V> create() {
74 return new HashBasedTable<R, C, V>(
75 new HashMap<R, Map<C, V>>(), new Factory<C, V>(0));
76 }
77
78
79
80
81
82
83
84
85
86
87 public static <R, C, V> HashBasedTable<R, C, V> create(
88 int expectedRows, int expectedCellsPerRow) {
89 checkNonnegative(expectedCellsPerRow, "expectedCellsPerRow");
90 Map<R, Map<C, V>> backingMap =
91 Maps.newHashMapWithExpectedSize(expectedRows);
92 return new HashBasedTable<R, C, V>(
93 backingMap, new Factory<C, V>(expectedCellsPerRow));
94 }
95
96
97
98
99
100
101
102
103
104 public static <R, C, V> HashBasedTable<R, C, V> create(
105 Table<? extends R, ? extends C, ? extends V> table) {
106 HashBasedTable<R, C, V> result = create();
107 result.putAll(table);
108 return result;
109 }
110
111 HashBasedTable(Map<R, Map<C, V>> backingMap, Factory<C, V> factory) {
112 super(backingMap, factory);
113 }
114
115
116
117 @Override public boolean contains(
118 @Nullable Object rowKey, @Nullable Object columnKey) {
119 return super.contains(rowKey, columnKey);
120 }
121
122 @Override public boolean containsColumn(@Nullable Object columnKey) {
123 return super.containsColumn(columnKey);
124 }
125
126 @Override public boolean containsRow(@Nullable Object rowKey) {
127 return super.containsRow(rowKey);
128 }
129
130 @Override public boolean containsValue(@Nullable Object value) {
131 return super.containsValue(value);
132 }
133
134 @Override public V get(@Nullable Object rowKey, @Nullable Object columnKey) {
135 return super.get(rowKey, columnKey);
136 }
137
138 @Override public boolean equals(@Nullable Object obj) {
139 return super.equals(obj);
140 }
141
142 @Override public V remove(
143 @Nullable Object rowKey, @Nullable Object columnKey) {
144 return super.remove(rowKey, columnKey);
145 }
146
147 private static final long serialVersionUID = 0;
148 }